
package com.hlkj.pay.controller.order;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import java.util.Collections;
import java.util.List;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.hlkj.framework.common.pojo.CommonResult;
import com.hlkj.framework.common.pojo.PageResult;
import com.hlkj.framework.common.util.collection.CollectionUtils;
import com.hlkj.pay.app.common.IDownloadExcelService;
import com.hlkj.pay.app.merchant.ext.impl.wxpay.dto.WxComplaintHistoryDto;
import com.hlkj.pay.common.CommonResultCode;
import com.hlkj.pay.controller.BaseController;
import com.hlkj.pay.dto.order.ComplaintOrderChannelQueryDto;
import com.hlkj.pay.dto.order.ComplaintOrderQueryDto;
import com.hlkj.pay.infrastructure.model.merchant.MerchantInfoDO;
import com.hlkj.pay.infrastructure.model.order.OrderComplaintChannelDO;
import com.hlkj.pay.infrastructure.model.order.OrderComplaintDO;
import com.hlkj.pay.infrastructure.model.pay.PayChannelDO;
import com.hlkj.pay.infrastructure.model.pay.ProviderInfoDO;
import com.hlkj.pay.service.merchant.IMerchantService;
import com.hlkj.pay.service.order.IOrderComplaintChannelService;
import com.hlkj.pay.service.order.IOrderComplaintService;
import com.hlkj.pay.service.pay.IPayChannelService;
import com.hlkj.pay.service.pay.IProviderService;
import com.hlkj.pay.util.DateUtils;
import com.hlkj.pay.vo.order.req.*;
import com.hlkj.pay.vo.order.resp.OrderComplaintChannelResp;
import com.hlkj.pay.vo.order.resp.export.OrderComplaintExport;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.util.StrUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.dreamlu.mica.core.utils.BeanUtil;

/**
 * @author HlpayTeam
 * @date 2024/10/23 11:36
 */
@Tag(name = "订单投诉管理")
@RestController
@RequestMapping("/v1/order/complaint")
@RequiredArgsConstructor
@Slf4j
public class ComplaintOrderController extends BaseController {

    private final IOrderComplaintChannelService orderComplaintChannelService;

    private final IOrderComplaintService orderComplaintService;

    private final IProviderService providerService;

    private final IPayChannelService payChannelService;

    private final IDownloadExcelService downloadExcelService;

    private final IMerchantService merchantService;

    @Operation(summary = "投诉列表")
    @PostMapping("/page")
    CommonResult<PageResult<OrderComplaintDO>> queryComplaintPage(@RequestBody ComplaintOrderPageQueryReq complaintOrderPageQueryReq) {
        ComplaintOrderQueryDto complaintOrderQueryDto = BeanUtil.copy(complaintOrderPageQueryReq, ComplaintOrderQueryDto.class);
        if(complaintOrderPageQueryReq.getQueryType() != null){
            // 查询类型 1：投诉单号 2：商户单号 3：交易单号 4:微信订单号
            switch (complaintOrderPageQueryReq.getQueryType()){
                case 1:
                    complaintOrderQueryDto.setComplaintId(complaintOrderPageQueryReq.getQueryValue());
                    break;
                case 2:
                    complaintOrderQueryDto.setMchOrderNo(complaintOrderPageQueryReq.getQueryValue());
                    break;
                case 3:
                    complaintOrderQueryDto.setPayOrderNo(complaintOrderPageQueryReq.getQueryValue());
                    break;
                case 4:
                    complaintOrderQueryDto.setWxOrderNo(complaintOrderPageQueryReq.getQueryValue());
                    break;
            }
        }
        processQueryDate(complaintOrderPageQueryReq, complaintOrderQueryDto);
        // 导出
        if (complaintOrderPageQueryReq.getExport() == 1) {
            String fileName = complaintOrderPageQueryReq.getExportName();
            if(StrUtil.isEmpty(fileName)){
                fileName = "订单投诉导出_" + DateUtils.getDateString(DatePattern.PURE_DATETIME_PATTERN);
            }
            downloadExcelService.createExcel(fileName, complaintOrderQueryDto, OrderComplaintExport.class);
            return CommonResult.success();
        }
        PageResult<OrderComplaintDO> page = orderComplaintService.queryPage(complaintOrderQueryDto);
        List<OrderComplaintDO> list = page.getList();
        if (CollectionUtils.isEmpty(list)) {
            return buildPageResult(page, Collections.emptyList());
        }
        for(OrderComplaintDO orderComplaintDO:list){
            if(StrUtil.isNotEmpty(orderComplaintDO.getChannelCode())){
                PayChannelDO payChannelDO = payChannelService.queryByCode(orderComplaintDO.getChannelCode());
                if(payChannelDO != null){
                    orderComplaintDO.setChannelName(payChannelDO.getName());
                }
            }
            if(StrUtil.isNotEmpty(orderComplaintDO.getProviderCode())){
                ProviderInfoDO providerInfoDO = providerService.queryByCode(orderComplaintDO.getProviderCode());
                if(providerInfoDO != null){
                    orderComplaintDO.setProviderName(providerInfoDO.getShortName());
                }
            }
            if(StrUtil.isNotEmpty(orderComplaintDO.getSn())){
                MerchantInfoDO merchantInfoDO = merchantService.queryMerchant(orderComplaintDO.getSn());
                if(merchantInfoDO != null){
                    orderComplaintDO.setSnName(merchantInfoDO.getName());
                }
            }
        }
        return buildPageResult(page, list);
    }

    @Operation(summary = "回复用户")
    @PostMapping("/replyUser")
    CommonResult<Void> replyUser(@RequestBody ComplaintReplyUserReq complaintReplyUserReq) {
        return orderComplaintService.replyUser(complaintReplyUserReq);
    }

    @Operation(summary = "投诉处理完成")
    @PostMapping("/complete")
    CommonResult<Void> complete(@RequestBody ComplaintCompleteReq complaintCompleteReq) {
        return orderComplaintService.complete(complaintCompleteReq);
    }

    @Operation(summary = "投诉详情")
    @GetMapping("/detail")
    CommonResult<OrderComplaintDO> detail(@RequestParam("id") Long id) {
        OrderComplaintDO orderComplaintDO = orderComplaintService.queryDetailAndSync(id);
        return CommonResult.success(orderComplaintDO);
    }

    @Operation(summary = "投诉删除")
    @PostMapping("/delete")
    CommonResult<Void> deleteComplain(@RequestBody ComplaintDeleteReq complaintDeleteReq) {
        orderComplaintService.delBatch(complaintDeleteReq);
        return CommonResult.success();
    }

    @Operation(summary = "查询投诉回复历史")
    @PostMapping("/queryHistoryPage")
    CommonResult<PageResult<WxComplaintHistoryDto>> queryHistoryPage(@RequestBody ComplaintReplyHistoryQueryReq complaintReplyHistoryQueryReq) {
        ComplaintOrderQueryDto complaintOrderQueryDto = BeanUtil.copy(complaintReplyHistoryQueryReq, ComplaintOrderQueryDto.class);
        processQueryDate(complaintReplyHistoryQueryReq, complaintOrderQueryDto);
        PageResult<WxComplaintHistoryDto> page = orderComplaintService.queryHistoryPage(complaintOrderQueryDto);
        if(page == null){
            return buildPageResult(PageResult.empty(Long.valueOf(complaintReplyHistoryQueryReq.getPageNo()) ,Long.valueOf(complaintReplyHistoryQueryReq.getPageSize())), Collections.emptyList());
        }
        List<WxComplaintHistoryDto> list = page.getList();
        if (CollectionUtils.isEmpty(list)) {
            return buildPageResult(page, Collections.emptyList());
        }
        return CommonResult.success(page);
    }

    @Operation(summary = "投诉渠道列表")
    @PostMapping("/channel/page")
    CommonResult<PageResult<OrderComplaintChannelDO>> queryComplaintChannelPage(@RequestBody ComplaintOrderChannelPageQueryReq complaintOrderChannelPageQueryReq) {
        ComplaintOrderChannelQueryDto complaintOrderChannelQueryDto = new ComplaintOrderChannelQueryDto();
        BeanUtil.copy(complaintOrderChannelPageQueryReq, complaintOrderChannelQueryDto);
        processQueryDate(complaintOrderChannelPageQueryReq, complaintOrderChannelQueryDto);
        PageResult<OrderComplaintChannelDO> page = orderComplaintChannelService.queryPage(complaintOrderChannelQueryDto);
        List<OrderComplaintChannelDO> list = page.getList();
        if (CollectionUtils.isEmpty(list)) {
            return buildPageResult(page, Collections.emptyList());
        }
        return buildPageResult(page, list);
    }

    @Operation(summary = "投诉渠道详情")
    @GetMapping("/channel/detail")
    CommonResult<OrderComplaintChannelResp> queryComplaintChannelDetail(@RequestParam("id") Long id) {
        return orderComplaintChannelService.queryChannelDetail(id);
    }

    @Operation(summary = "投诉渠道新增")
    @PostMapping("/channel/add")
    CommonResult<Void> addComplaintChannel(@Validated @RequestBody ComplaintChannelAddReq complaintOrderChannelReq) {
        OrderComplaintChannelDO orderComplaintChannelDO = new OrderComplaintChannelDO();
        BeanUtil.copy(complaintOrderChannelReq, orderComplaintChannelDO);
        return orderComplaintChannelService.addComplaintChannel(orderComplaintChannelDO);
    }

    @Operation(summary = "投诉渠道修改")
    @PostMapping("/channel/update")
    CommonResult<Void> updateComplaintChannel(@RequestBody ComplaintOrderChannelUpdateReq complaintOrderChannelReq) {
        OrderComplaintChannelDO orderComplaintChannelDO = BeanUtil.copy(complaintOrderChannelReq, OrderComplaintChannelDO.class);
        return orderComplaintChannelService.updateOrderComplaintChannel(orderComplaintChannelDO);
    }

    @Operation(summary = "投诉渠道删除")
    @GetMapping("/channel/delete")
    CommonResult<Void> deleteComplaintChannel(@RequestParam(value="id") Long id) {
        orderComplaintChannelService.delOrderComplaintChannel(id);
        return CommonResult.success();
    }

    @Operation(summary = "投诉渠道状态更新")
    @PostMapping("/channel/status")
    CommonResult<Void> updatecomplaintChannelStatus(@RequestBody ComplaintChannelStatusReq complaintOrderChannelReq) {
        if(complaintOrderChannelReq.getStatus() == null){
            return CommonResult.error(CommonResultCode.PARAMETER_MISSING);
        }
        OrderComplaintChannelDO orderComplaintChannelDO = new OrderComplaintChannelDO();
        orderComplaintChannelDO.setId(complaintOrderChannelReq.getId());
        orderComplaintChannelDO.setStatus(complaintOrderChannelReq.getStatus());
        orderComplaintChannelService.updateOrderComplaintChannel(orderComplaintChannelDO);
        return CommonResult.success();
    }

}
